//
//  Game.m
//  Find the difference
//
//  Created by administrator on 16/8/13.
//  Copyright © 2016年 duanzhenglong. All rights reserved.
//

#import "Game.h"
#import "Menu.h"
#import "SldeSlipMenu.h"
#import "GameBegain.h"
#import "AppDelegate.h"
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]//宏定义颜色
@interface Game (){
    UIView*ContentView;
    int Side;
    int Space;
    int margin;
    int temp;
    int T;
    int Score;
    CGFloat alpha;
}
@property(nonatomic,retain)NSTimer*timer;
@end
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
@implementation Game

- (void)viewDidLoad {
    [super viewDidLoad];
    alpha=0.3;
    temp=2;T=10;Score=0;
    self.Score.text=[NSString stringWithFormat:@"%d",Score];
    self.Time.text=[NSString stringWithFormat:@"%ds",T];
    self.Pause2.enabled=NO;
    [self CreatButton:temp];
}
-(void)CreatButton:(int)Num{
    //父类视图
    ContentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,Width-20,Width-20 )];
    ContentView.center=CGPointMake(Width/2, Height/2+20);
    ContentView.layer.cornerRadius=10;
    ContentView.layer.masksToBounds=YES;
    ContentView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:ContentView];
    margin=6;
    Space=2;
    Side=(ContentView.frame.size.width-(Num-1)*Space-2*margin)/Num;
    
    //创建button
    UIColor*randomColor=(UIColor*)RGBA(arc4random()%255, arc4random()%255, arc4random()%255, 1);
    NSInteger tmp=0;
    for (int i=0; i<Num; i++) {
        for (int j=0; j<Num; j++) {
            UIButton*Btn=[[UIButton alloc]initWithFrame:CGRectMake(margin+ j*Side+j*Space, margin+ i*Space+i*Side, Side, Side)];
            [ContentView addSubview:Btn];
            [Btn setBackgroundColor:randomColor];
            Btn.layer.cornerRadius=6;
            Btn.layer.masksToBounds=YES;
            Btn.tag=tmp;
            [Btn addTarget:self action:@selector(AddButtonNum:) forControlEvents:UIControlEventTouchUpInside];
            tmp++;
        }
    }
    UIButton*randomButton=(UIButton*)[ContentView viewWithTag:arc4random()%(Num*Num)];
    if (alpha<0.9) {
        alpha+=0.15;
        randomButton.backgroundColor=[randomColor colorWithAlphaComponent:alpha];
    }else{
        randomButton.backgroundColor=[randomColor colorWithAlphaComponent:0.8];
    }
    randomButton.tag=300;
}

-(void)AddButtonNum:(UIButton*)Btn{
    self.Pause2.enabled=YES;
    if (temp==2) {
        [self addTimer];
    }
    if (Btn.tag==300) {
        [ContentView removeFromSuperview];
        self.Score.text=[NSString stringWithFormat:@"%d",Score+=2];
        [self CreatButton:++temp];
    }else{
        if (Score>0) {
            self.Score.text=[NSString stringWithFormat:@"%d",--Score];
        }
    }
}
- (IBAction)Back:(UIButton *)sender {
    GameBegain*G1=[[GameBegain alloc]init];
    Menu*M1=[[Menu alloc]init];
    SldeSlipMenu*S1=[[SldeSlipMenu alloc]initWithView:M1 andMainView:G1];
    S1.sideslipTapGes.enabled=YES;
    [self presentViewController:S1 animated:YES completion:nil];
}
/////////////////////////////////////
//暂停、开始
- (IBAction)Pause:(UIButton *)sender {
    UIView*MasksView=[[UIView alloc]initWithFrame:ContentView.frame];
    MasksView.center=CGPointMake(ContentView.frame.size.width/2, ContentView.frame.size.height/2);
    MasksView.backgroundColor=[[UIColor grayColor]colorWithAlphaComponent:0.8];
    UILabel*Label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
    Label.center=CGPointMake(MasksView.frame.size.width/2, MasksView.frame.size.height/2);
    Label.text=@"休息会吧.....";
    [MasksView addSubview:Label];
    MasksView.tag=400;
    MasksView.userInteractionEnabled=NO;
    if (sender.selected==NO) {
        sender.selected=YES;
        ContentView.userInteractionEnabled=NO;
        [ContentView addSubview:MasksView];
        [self removeTimer];
    }else{
        sender.selected=NO;
        ContentView.userInteractionEnabled=YES;
        UIView*View=(UIView*)[ContentView viewWithTag:400];
        [View removeFromSuperview];
        [self addTimer];
    }
}
////////////////////////////////////////////
//定时器
-(void)addTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ChangeTime) userInfo:nil repeats:YES];
}
-(void)removeTimer{
    [self.timer invalidate];
    self.timer=nil;
}
-(void)ChangeTime{
    if (T>0) {
        self.Time.text=[NSString stringWithFormat:@"%ds",--T];
    }
    else{
        [self removeTimer];
        NSString*str=[[NSString alloc]init];
        int score=[self.Score.text intValue];
        if (score<12) {
            str=[NSString stringWithFormat:@"你是色盲！"];
        }else if (score>=12&&score<28){
            str=[NSString stringWithFormat:@"你是色弱！"];
        }else if (score>=28&&score<30){
            str=[NSString stringWithFormat:@"你是色童！"];
        }else if (score>=30&&score<40){
            str=[NSString stringWithFormat:@"你是色人！"];
        }
        ///////////////////////////////////////
        [self TransitionValue:score];
        
        UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"经鉴定：" message:str preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction*OK=[UIAlertAction actionWithTitle:@"返回主菜单" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            GameBegain*G1=[[GameBegain alloc]init];
            Menu*M1=[[Menu alloc]init];
            SldeSlipMenu*S1=[[SldeSlipMenu alloc]initWithView:M1 andMainView:G1];
            S1.sideslipTapGes.enabled=YES;
            [self presentViewController:S1 animated:YES completion:nil];
        }];
        UIAlertAction*replay=[UIAlertAction actionWithTitle:@"再来一次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            T=10;Score=0;
            self.Time.text=@"10s";
            self.Score.text=@"0";
            temp=2;
            [ContentView removeFromSuperview];
            [self CreatButton:temp];
        }];
        [alert addAction:OK];
        [alert addAction:replay];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
//////////////////////////////////////////////////////////////////////////////////
//传值
-(void)TransitionValue:(int)score{
    //获取当前时间
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    //将当前时间和分数存到沙盒中
    NSString*path=[NSString stringWithFormat:@"%@/Documents/myScore.plist",NSHomeDirectory()];
    NSMutableArray*Arr=[NSMutableArray arrayWithContentsOfFile:path];
    NSString*str=[NSString stringWithFormat:@"%d",score];
    NSDictionary*dic=@{@"time":locationString,@"score":str};
    [Arr addObject:dic];
    [Arr writeToFile:path atomically:YES];
    //传值
    AppDelegate*HighScore=[[UIApplication sharedApplication]delegate];
    if (score>HighScore.heightestScore1||HighScore.heightestScore1==0) {
        HighScore.heightestScore3=HighScore.heightestScore2;
        HighScore.heightestScore2=HighScore.heightestScore1;
        HighScore.heightestScore1=score;
        HighScore.highName3=HighScore.highName2;
        HighScore.highName2=HighScore.highName1;
        HighScore.highName1=HighScore.NAME;
    }else if (score>HighScore.heightestScore2||HighScore.heightestScore2==0){
        HighScore.heightestScore3=HighScore.heightestScore2;
        HighScore.heightestScore2=score;
        HighScore.highName3=HighScore.highName2;
        HighScore.highName2=HighScore.NAME;
    }else if (score>HighScore.heightestScore3||HighScore.heightestScore3==0){
        HighScore.heightestScore3=score;
        HighScore.highName3=HighScore.NAME;
    }
}
@end
